package android.slc.module.component.attchment.domain;

import android.slc.base.api.slc.callback.SlcObserver;
import android.slc.base.api.slc.callback.po.SlcEntity;
import android.slc.base.ui.dialog.SlcFastDialogUtils;
import android.slc.fast.vd.list.BaseListVmSub;
import android.slc.box.attachment.bean.Progress;
import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import android.slc.box.core.property.SimpleWrapObjectProperty;
import android.slc.box.core.property.WrapDataChangeListener;
import android.slc.module.component.attchment.entity.Attachment;
import android.slc.module.component.attchment.entity.StorageForm;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.annotations.NonNull;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

public class AttachmentReviewVmSub<T extends AttachmentReviewVmSub.AttachmentAdapter> extends BaseListVmSub<Attachment> {
    protected Long[] ids;//关联的id
    private List<StorageForm> storageFormList;
    public final SimpleWrapObjectProperty<Boolean> showAttEmptyHint = new SimpleWrapObjectProperty<>(true);
    protected final SimpleWrapObjectProperty<Attachment> notifyAttItemDataChangeLd = new SimpleWrapObjectProperty<>();
    private T attachmentAdapter;

    public AttachmentReviewVmSub() {
        getDataListObserver().addListener(new WrapDataChangeListener<List<Attachment>>() {
            @Override
            public void onChanged(List<Attachment> value) {
                onDataListChangedCallback(getDataList());
            }
        });
    }

    protected void onDataListChangedCallback(List<Attachment> dataList) {
        showAttEmptyHint.setValuePlus(CollectionUtils.isEmpty(dataList));
    }

    public void setAttachmentAdapter(T attachmentAdapter) {
        this.attachmentAdapter = attachmentAdapter;
        this.attachmentAdapter.setAttachmentReviewVmBox(this);
    }

    public AttachmentAdapter getAttachmentAdapter() {
        return attachmentAdapter;
    }

    public SimpleWrapObjectProperty<Attachment> getNotifyAttItemDataChangeLd() {
        return notifyAttItemDataChangeLd;
    }

    public int getNotifyAttItemDataChangeByIndex() {
        return getDataList().indexOf(notifyAttItemDataChangeLd.getValuePlus());
    }

    /**
     * 初始化
     *
     * @param ids
     */
    public void setAttIds(Long[] ids) {
        this.ids = ids;
    }

    public void setAttList(List<Attachment> attachmentList) {
        getDataListSlcObserver().onNext(SlcEntity.getSlcEntityByData(attachmentList));
    }

    /**
     * 初始化
     *
     * @param ids
     */
    public void setAttIdsAndFind(Long[] ids) {
        setAttIds(ids);
        refresh();
    }

    /**
     * 使用storageFormList初始化
     *
     * @param storageFormList
     */
    public void setStorageFormList(@NonNull List<StorageForm> storageFormList) {
        this.storageFormList = storageFormList;
        refresh();
    }

    public List<StorageForm> getStorageFormList() {
        return storageFormList;
    }

    @Override
    public void refresh() {
        super.refresh();
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onRefresh(getDataListSlcObserver());
        }
    }

    @Override
    public void itemClick(int index, Attachment data) {
        if (data.isLocalBody()) {
            onPreview(data);
            return;
        }
        Progress progress = data.getProgress();
        if (data.isNetBody() && progress != null && (progress.status == Progress.WAITING || progress.status == Progress.LOADING)) {
            SlcFastDialogUtils.showPrompt(SlcResourceUtils.getString("app_label_please_wait_for_the_download_to_complete"));
            //PromptView.show(SlcResourceUtils.getString("app_label_please_wait_for_the_download_to_complete"));
        } else {
            onDownload(data);
        }
    }

    protected void onPreview(Attachment attachment) {
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onPreview(attachment);
        }

    }

    public void onDownload(Attachment attachment) {
        if (getAttachmentAdapter() != null) {
            getAttachmentAdapter().onDownload(attachment);
        }
    }

    public void removeAtt(int index) {
        if (index > 0 && getDataList().size() > index) {
            getDataList().remove(index);
        }
    }

    public void removeAtt(Attachment attachment) {
        getDataList().remove(attachment);
        dataListOf.notifyChange();
    }

    public interface AttachmentAdapter {
        void setAttachmentReviewVmBox(AttachmentReviewVmSub<? extends AttachmentAdapter> attachmentVmBox);

        void onPreview(Attachment attachment);

        void onDownload(Attachment attachment);

        void onRefresh(SlcObserver<List<Attachment>> observer);

        void uploadAttachmentSingleDirectlyUnder(Attachment attachment);

        Observable<Attachment> uploadAttachmentSingle(Attachment attachment);

        Observable<Attachment> uploadAttachment();
    }
}
